package nullby.omnia.activity;

import nullby.common.utils.CustomLog;
import nullby.omnia.R;
import nullby.omnia.constaints.ApplicationConstaints;
import nullby.omnia.fragment.FragmentSettings;
import nullby.omnia.fragment.FragmentTheme;
import nullby.omnia.fragment.FragmentVocabulare;
import nullby.omnia.ui.SpinnerTranslation;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.ArrayAdapter;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class ActivityMain extends SherlockFragmentActivity implements
		ActionBar.OnNavigationListener {
	public static final String TAG = ActivityMain.class.getSimpleName();

	private static String[] views;

	private Context context;
	private ArrayAdapter<CharSequence> viewAdapter;
	FragmentTheme fragmentTheme = FragmentTheme.newInstance();
	FragmentVocabulare fragmentVoc = FragmentVocabulare.newInstance();
	FragmentSettings fragmentSettings = FragmentSettings.newInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		CustomLog.logI(TAG, "onCreate");
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		views = getResources().getStringArray(R.array.actionbar_locations);
		initView();
	}

	protected void initView() {
		CustomLog.logI(TAG, "initView");

		context = getSupportActionBar().getThemedContext();
		viewAdapter = new ArrayAdapter<CharSequence>(context,
				R.layout.sherlock_spinner_item, views);

		viewAdapter
				.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

		initActionBar();
	}

	private void initActionBar() {
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		getSupportActionBar().setListNavigationCallbacks(viewAdapter, this);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setCustomView(new SpinnerTranslation(this));
		getSupportActionBar().setDisplayShowCustomEnabled(true);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		CustomLog.logI(TAG, "onNavigationItemSelected");

		FragmentTransaction fragmentTransaction = getSupportFragmentManager()
				.beginTransaction();
		switch (itemPosition) {
		case ApplicationConstaints.ID_CARD:
			getSupportActionBar().setDisplayShowCustomEnabled(true);
			fragmentTransaction.replace(R.id.fragmentView, fragmentTheme);

			break;
		case ApplicationConstaints.ID_VOCABULARE:
			getSupportActionBar().setDisplayShowCustomEnabled(true);
			fragmentTransaction.replace(R.id.fragmentView, fragmentVoc);
			break;
		case ApplicationConstaints.ID_SETTINGS:
			getSupportActionBar().setDisplayShowCustomEnabled(false);
			fragmentTransaction.replace(R.id.fragmentView, fragmentSettings);
			break;
		default:
			break;
		}
		fragmentTransaction.commit();
		return true;
	}
}
